package com.kylin.core.merchant.event.repository;

import com.kylin.dal.entity.commodity.CommentRepository;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;

/**
 * 操作结算中心结算表的数据查询功能
 * @author Damon S.
 * @version v1.0.1
 * @date 2019年11月12日 15:13
 */
@Slf4j
@Component
public class RealtimeOrderingDataHandler {

    private final JPAQueryFactory hdsSettlementQueryFactory;
//    private final QSettlementPolicyEntry qSettlementPolicyEntry;
//    private final QOrderHeaderEntry qOrderHeaderEntry;
//    private final QOrderDetailEntry qOrderDetailEntry;
//    private final QPerformanceEntry qPerformanceEntry;
//    private final QFreshmanEntry qFreshmanEntry;
    private final CommentRepository commentRepository;

    private static final int POLICY_NOT_REMOVED = 0;
    private static final int ORDER_NOT_REMOVED = 1;
    private static final int RECORD_LIMITATION = 100;

    public RealtimeOrderingDataHandler(/*@Qualifier("hdsSettlementEntityManager")*/EntityManager entityManager,
                                                                                   CommentRepository commentRepository) {
        this.hdsSettlementQueryFactory = new JPAQueryFactory(entityManager);
//        this.qOrderHeaderEntry = QOrderHeaderEntry.orderHeaderEntry;
//        this.qPerformanceEntry = QPerformanceEntry.performanceEntry;
//        this.qSettlementPolicyEntry = QSettlementPolicyEntry.settlementPolicyEntry;
//        this.qOrderDetailEntry = QOrderDetailEntry.orderDetailEntry;
//        this.qFreshmanEntry = QFreshmanEntry.freshmanEntry;
        this.commentRepository = commentRepository;
    }

//    public GalleryEntry findOrderByNo(String orderNo) {
//        return hdsSettlementQueryFactory.select(defineOrderHeaderBean())
//                .from(qOrderHeaderEntry).leftJoin(qOrderDetailEntry)
//                .on(qOrderHeaderEntry.orderNo.eq(qOrderDetailEntry.orderNo))
//                .leftJoin(qFreshmanEntry)
//                .on(qOrderHeaderEntry.buyerCpId.eq(qFreshmanEntry.newComerCpId))
//                .where(qOrderHeaderEntry.orderNo.eq(orderNo)
//                        .and(qOrderHeaderEntry.status.in(Arrays.asList(1, 3, 4, 5)))
//                        .and(qOrderHeaderEntry.subChannel.in(Arrays.asList(1, 2, 3, 7)))
//                        .and(qOrderHeaderEntry.removed.eq(ORDER_NOT_REMOVED)))
//                .fetchOne();
//    }
//
//    /**
//     * 查询未结算完成的订单数据
//     * @return 返回订单数据
//     */
//    public List<GalleryEntry> findOrdersUnsettled(UnsettledOrdersParams params) {
//        return hdsSettlementQueryFactory.select(defineOrderHeaderBean())
//                .from(qOrderHeaderEntry).leftJoin(qOrderDetailEntry)
//                .on(qOrderHeaderEntry.orderNo.eq(qOrderDetailEntry.orderNo))
//                .leftJoin(qFreshmanEntry)
//                .on(qOrderHeaderEntry.buyerCpId.eq(qFreshmanEntry.newComerCpId))
//                .where(qOrderHeaderEntry.status.in(Arrays.asList(1, 3, 4, 5))
//                        .and(qOrderHeaderEntry.subChannel.in(Arrays.asList(1, 2, 3, 7)))
//                        .and(qOrderHeaderEntry.id.gt(params.getMaxOrderId()))
//                        .and(qOrderHeaderEntry.createdAt.after(Timestamp.valueOf("2019-10-01 00:00:00")))
//                        .and(qOrderHeaderEntry.removed.eq(ORDER_NOT_REMOVED)))
//                .groupBy(qOrderHeaderEntry.id)
//                .orderBy(qOrderHeaderEntry.finishPaidAt.asc())
//                .limit(RECORD_LIMITATION)
//                .fetch();
//    }
//
//    /**
//     * 查询结算规则数据
//     * @return 返回规则列表
//     */
//    public List<GroupCommodityEntry> findSettlementRules() {
//        return hdsSettlementQueryFactory.selectFrom(qSettlementPolicyEntry)
//                .where(qSettlementPolicyEntry.removed.eq(POLICY_NOT_REMOVED))
//                .orderBy(qSettlementPolicyEntry.policyId.asc())
//                .fetch();
//    }
//
//    public void testMethod() {
//        CommentEntry entry = CommentEntry.builder()
//                .orderNo("")
//                .hdsPpvCpId(3000000L)
//                .build();
//        commentRepository.saveAndFlush(entry);
//    }
//
//    /**
//     * 保存晋升数据
//     * @param promotion 晋升结果数据
//     */
//    public void persistPromotion(CommodityGallery promotion) {
//        // TODO: 暂未实现
//        throw new NotImplementedException();
//    }
//
//    /**
//     * 保存归属数据
//     * @param ascription 归属结果数据
//     */
//    public void persistAscription(PerformanceAscription ascription) {
//        // TODO: 暂未实现
//        throw new NotImplementedException();
//    }
//
//    /**
//     * 保存拨付数据
//     * @param rewards 拨付结果数据
//     */
//    public void persistRewards(List<CommodityComment> rewards) {
//        // TODO: 暂未实现
//        throw new NotImplementedException();
//    }
//
//    private QBean<GalleryEntry> defineOrderHeaderBean() {
//        return Projections.bean(GalleryEntry.class,
//                qOrderHeaderEntry.id,
//                qOrderHeaderEntry.status,
//                qOrderHeaderEntry.vp,
//                qOrderHeaderEntry.buyerCpId,
//                qOrderHeaderEntry.fromCpId,
//                qOrderHeaderEntry.groupCpId,
//                qOrderHeaderEntry.orderNo,
//                qOrderHeaderEntry.productAmount,
//                qOrderHeaderEntry.orderMonth,
//                qOrderHeaderEntry.commissionProductAmount,
//                qOrderHeaderEntry.activated,
//                qOrderHeaderEntry.commitPaidAt,
//                qOrderHeaderEntry.finishPaidAt,
//                qOrderHeaderEntry.supplemented,
//                qOrderHeaderEntry.joinType,
//                qOrderHeaderEntry.channel,
//                qOrderHeaderEntry.subChannel,
//                qOrderHeaderEntry.sourceId,
//                qOrderHeaderEntry.itemSum,
//                qOrderHeaderEntry.removed,
//                qOrderHeaderEntry.createdAt,
//                qOrderDetailEntry.orderNo.count().as("detailCount"),
//                qOrderDetailEntry.promoAmount.sum().as("promoAmount"),
//                qOrderDetailEntry.savingAmount.sum().as("savingAmount"),
//                qOrderDetailEntry.serveAmount.sum().as("serveAmount"),
//                qFreshmanEntry.newComerCpId.count().as("freshmanRewarded"));
//    }
//
//    @Value
//    @Builder
//    public static class UnsettledOrdersParams {
//        private final Long maxOrderId;
//        private final Timestamp paidAt;
//    }
}
